package com.geeklei.media.ui.utils;

import android.text.TextUtils;
import android.util.Log;

import org.xml.sax.SAXException;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

public class DateTimeHelper {
    private static final String LOG_TAG = "common/DateTimeHelper";
    static final String TAG = DateTimeHelper.class.getName();
    public static final TimeZone sBeijingTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
    public static final long sDayInMilliseconds = 86400000;
    public static final long sDayInMinutes = 1440;
    public static final long sHourInMilliseconds = 3600000;
    public static final long sHourInMinutes = 60;
    public static final long sMinuteInMilliseconds = 60000;

    public static final long getCurrentTiemstamp() {
        return Calendar.getInstance(sBeijingTimeZone).getTimeInMillis();
    }

    public static final long getTodayStartTimestamp() {
        return getTodayStartTimestamp(getCurrentTiemstamp());
    }

    public static final long getTodayStartTimestamp(long j) {
        return j - (j % 86400000);
    }

    public static final long getTomorrowStartTimestamp(long j) {
        return (j - (j % 86400000)) + 86400000;
    }

    public static final long getElapsedMinutesFromToday() {
        return getElapsedMinutesFromToday(getCurrentTiemstamp());
    }

    public static final long getElapsedMinutesFromToday(long j) {
        return (j - getTodayStartTimestamp(j)) / 60000;
    }

    public static final long getElapsedMinutesFromHour() {
        return getElapsedMinutesFromHour(getCurrentTiemstamp());
    }

    public static final long getElapsedMinutesFromHour(long j) {
        return getElapsedMinutesFromToday(j) % 60;
    }

    public static long parseDate(String str) throws SAXException {
        long j = -1;
        if (!TextUtils.isEmpty(str)) {
            GregorianCalendar gregorianCalendar = new GregorianCalendar();
            try {
                gregorianCalendar.setTime(new SimpleDateFormat("yyyy-MM-dd", Locale.US).parse(str));
                gregorianCalendar.setTimeZone(sBeijingTimeZone);
                j = gregorianCalendar.getTimeInMillis();
            } catch (Throwable e) {
                Log.e(LOG_TAG, "Failed to parse date", e);
            }
        }
        return j;
    }

    public static Date fromString(String str, String str2) {
        Date date = null;
        if (!TextUtils.isEmpty(str)) {
            try {
                date = new SimpleDateFormat(str2).parse(str);
            } catch (Throwable e) {
                Log.d(TAG, "", e);
            }
        }
        return date;
    }

    public static String toString(Date date, String str) {
        String str2 = "";
        if (date != null) {
            return new SimpleDateFormat(str).format(date);
        }
        return str2;
    }

    public static String getCurrentString(String str) {
        return getTimeString(System.currentTimeMillis(), str);
    }

    public static String getTimeString(long j, String str) {
        return new SimpleDateFormat(str).format(new Date(j));
    }
}
